package com.xie.smfs.client;

import com.xie.smfs.Smfs;
import com.xie.smfs.entity.GhostEntity;
import net.minecraft.client.model.*;
import net.minecraft.client.render.*;
import net.minecraft.client.render.entity.*;
import net.minecraft.client.render.entity.model.*;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.util.Identifier;

public class GhostRenderer extends BipedEntityRenderer<GhostEntity, BipedEntityModel<GhostEntity>> {
    private static final Identifier TEXTURE = new Identifier(Smfs.MOD_ID, "textures/entity/food_ghost.png");

    public GhostRenderer(EntityRendererFactory.Context ctx) {
        super(ctx, new BipedEntityModel<>(getModelPart(ctx)), 0.5f);
    }

    private static ModelPart getModelPart(EntityRendererFactory.Context ctx) {
        // 创建模型数据
        ModelData modelData = new ModelData();
        ModelPartData root = modelData.getRoot();

        // 添加玩家身体部件
        root.addChild("head", ModelPartBuilder.create().uv(0, 0).cuboid(-4, -8, -4, 8, 8, 8), ModelTransform.NONE);
        root.addChild("hat", ModelPartBuilder.create().uv(32, 0).cuboid(-4, -8, -4, 8, 8, 8, new Dilation(0.5f)), ModelTransform.NONE);
        root.addChild("body", ModelPartBuilder.create().uv(16, 16).cuboid(-4, 0, -2, 8, 12, 4), ModelTransform.NONE);
        root.addChild("right_arm", ModelPartBuilder.create().uv(40, 16).cuboid(-3, -2, -2, 4, 12, 4), ModelTransform.pivot(-5, 2, 0));
        root.addChild("left_arm", ModelPartBuilder.create().uv(40, 16).mirrored().cuboid(-1, -2, -2, 4, 12, 4), ModelTransform.pivot(5, 2, 0));
        root.addChild("right_leg", ModelPartBuilder.create().uv(0, 16).cuboid(-2, 0, -2, 4, 12, 4), ModelTransform.pivot(-1.9f, 12, 0));
        root.addChild("left_leg", ModelPartBuilder.create().uv(0, 16).mirrored().cuboid(-2, 0, -2, 4, 12, 4), ModelTransform.pivot(1.9f, 12, 0));

        // 将 ModelData 转换为 ModelPart
        return TexturedModelData.of(modelData, 64, 32).createModel();
    }

    @Override
    public Identifier getTexture(GhostEntity entity) {
        return TEXTURE;
    }

    @Override
    public void render(GhostEntity entity, float yaw, float tickDelta, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light) {
        if (!entity.isVisible()) {
            return;
        }
        // 设置半透明效果
        matrices.push();
        matrices.scale(1.2f, 1.2f, 1.2f); // 幽灵体型放大

        // 使用半透明渲染层
        VertexConsumer vertexConsumer = vertexConsumers.getBuffer(RenderLayer.getEntityTranslucent(TEXTURE));
        super.render(entity, yaw, tickDelta, matrices, vertexConsumers, light);

        matrices.pop();
    }
}